package com.zdw.demo.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.zdw.common.parent.JSONResult;
import com.zdw.demo.dto.CustomerSelectDto;
import com.zdw.demo.dto.CustomerUpdateDto;
import com.zdw.demo.config.international.InternationalUtils;
import com.zdw.demo.dto.CustomerAddDto;
import com.zdw.demo.entity.CustomerEntity;
import com.zdw.demo.service.CustomerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 客户信息 前端控制器
 * </p>
 *
 * @author 卓德文
 * @since 2023-04-05
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Resource
    private CustomerService customerService;

    /**
     * 新增客户信息
     *
     * @param customerAddDto
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增客户信息")
    @SaCheckLogin
    @SaCheckPermission(value = "customer.add", orRole = {"superAdmin", "admin", "customer"})
    public JSONResult save(@RequestBody @Valid CustomerAddDto customerAddDto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), customerService.save(customerAddDto));
    }

    /**
     * 删除客户信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除客户信息")
    @SaCheckLogin
    @SaCheckPermission(value = "customer.delete", orRole = {"superAdmin", "admin", "customer"})
    public JSONResult delete(@RequestParam Integer id) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), customerService.delete(id));
    }

    /**
     * 修改客户信息
     *
     * @param customerUpdateDto
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改客户信息")
    @SaCheckLogin
    @SaCheckPermission(value = "customer.update", orRole = {"superAdmin", "admin", "customer"})
    public JSONResult update(@RequestBody @Valid CustomerUpdateDto customerUpdateDto) {
        CustomerEntity customerEntity = BeanUtil.copyProperties(customerUpdateDto, CustomerEntity.class);
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), customerService.update(customerEntity));
    }

    /**
     * 查询单个客户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/select")
    @ApiOperation("查询单个客户信息")
    @SaCheckPermission(value = "customer.select", orRole = {"superAdmin", "admin", "customer"})
    public JSONResult select(@RequestParam Integer id) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.query.success"), customerService.selectOne(id));
    }

    /**
     * 查询已启用的客户信息列表
     *
     * @param customerSelectDto
     * @return
     */
    @PostMapping("/selectList")
    @ApiOperation("查询已启用的客户信息列表")
    @SaCheckPermission(value = "customer.select", orRole = {"superAdmin", "admin", "customer"})
    public JSONResult selectList(@RequestBody CustomerSelectDto customerSelectDto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.query.success"), customerService.selectList(customerSelectDto));
    }

    /**
     * 客户信息分页查询
     *
     * @param customerSelectDto
     * @return
     */
    @PostMapping("/page")
    @ApiOperation("客户信息分页查询")
    @SaCheckPermission(value = "customer.select", orRole = {"superAdmin", "admin", "customer"})
    public JSONResult page(@RequestBody CustomerSelectDto customerSelectDto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.query.success"), customerService.page(customerSelectDto));
    }

    public static void main(String[] args) {
        int[] arr = {4,3,6,2,7,5,1,8,9};

        //sort(arr);
        //System.out.println("冒泡排序后的数组："+ JSON.toJSONString(arr));

        selectSort(arr);
        System.out.println("选择排序后的数组："+ JSON.toJSONString(arr));

        int num = select(arr, 6);
        System.out.println("二分法方法的结果："+ num);
    }

    /**
     * 冒泡排序
     * @param arr
     */
    public static void sort(int[] arr){
        int temp = 0;
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if(arr[j] > arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }

    /**
     * 二分法查找
     * @param arr
     * @param key
     * @return
     */
    public static int select(int[] arr,int key){
        int low = 0;
        int high = arr.length - 1;
        while(low <= high) {
            int midle = (low + high) / 2;
            if(arr[midle] > key){
                high = midle - 1;
            }else if(arr[midle] < key){
                low = midle + 1;
            }else {
                return midle;
            }
        }
        return -1;
    }

    /**
     * 选择排序
     * @param arr
     */
    public static void selectSort(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            int minIndex = i;
            for (int j = i+1; j < arr.length; j++) {
                if(arr[minIndex] > arr[j]){
                    minIndex = j;
                }
            }
            if(i != minIndex){
                int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            }
        }
    }

}
